package com.mdlib.helper.module.send.profile.fragments;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.ljlib.help.R;
import com.mdlib.helper.api.DataManager;
import com.mdlib.helper.api.network.RxHelper;
import com.mdlib.helper.api.network.RxSubscribe;
import com.mdlib.helper.base.BaseTitleFragment;
import com.mdlib.helper.model.entity.ProblemEntity;
import com.mdlib.helper.model.entity.WebViewEntity;
import com.mdlib.helper.ui.UIHelper;
import com.mdlib.helper.ui.adapter.ProblemAdapter;
import com.mdlib.helper.ui.widget.springview.HelperHeads;
import com.mdlib.helper.ui.widget.springview.SpringView;
import com.mdlib.helper.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * 作者：HQY on 16/11/15 17:21
 * 邮箱：hqy_xz@126.com
 */

public class ProblemFragment extends BaseTitleFragment {


    @Bind(R.id.rv_problem_list)
    RecyclerView mRvProblemList;
    @Bind(R.id.sv_problem)
    SpringView mSvProblem;


    private List<ProblemEntity> problemList = new ArrayList<>();

    private ProblemAdapter mProblemAdapter;

    public static ProblemFragment newInstance() {
        return new ProblemFragment();
    }


    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_problem;
    }

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
        setTitle(R.string.me_tv_problem, R.color.white)
                .setTitleBgColor(R.color.generic_view_bg)
                .setImgLeftListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        removeFragment();
                    }
                });
        init();

    }

    public void init() {
        mProblemAdapter = new ProblemAdapter(problemList);
        mRvProblemList.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRvProblemList.setAdapter(mProblemAdapter);
        mRvProblemList.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void SimpleOnItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {

            }

            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                UIHelper.showWeb(getActivity(), new WebViewEntity(problemList.get(position)));
            }
        });
        mSvProblem.setType(SpringView.Type.FOLLOW);
        mSvProblem.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                getProblem();
            }

            @Override
            public void onLoadmore() {

            }
        });
        mSvProblem.setHeader(new HelperHeads(getActivity()));
        getProblem();

    }


    public void getProblem() {
        startLoadingDialog(true);
        addSubscribe(DataManager.getInstance().getUserApi()
                .getProblem().compose(RxHelper.<ArrayList<ProblemEntity>>defaultHandleResult())
                .subscribe(new RxSubscribe<ArrayList<ProblemEntity>>() {
                    @Override
                    protected void onSucc(ArrayList<ProblemEntity> problemEntities) {
                        stopLoadingDialog();
                        problemList = problemEntities;
                        mProblemAdapter.setNewData(problemList);
                        mProblemAdapter.notifyDataSetChanged();
                        mSvProblem.onFinishFreshAndLoad();
                    }

                    @Override
                    protected void onFail(String msg) {
                        stopLoadingDialog();
                        ToastUtil.showToast(msg);
                        mSvProblem.onFinishFreshAndLoad();
                    }
                }));
    }


}
